/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */

define(['N/ui/serverWidget', 'N/record', 'N/search'], function (ui, record, search) {
    function onRequest(context) {
        var form = ui.createForm({title: '条形码扫描界面'});
        form.clientScriptModulePath = './cs_woc_scan.js';


        form.addFieldGroup({id: 'fieldgroupid1', label: '条码'});
        var woBarcodeInput = form.addField({
            id: 'custpage_wo_barcodescan',
            type: ui.FieldType.TEXT,
            label: '工单标签扫码框',
            container: 'fieldgroupid1'
        });
        woBarcodeInput.isMandatory = true;
        var processBarcodeInput = form.addField({
            id: 'custpage_process_barcodescan',
            type: ui.FieldType.TEXT,
            label: '工序标签扫码框',
            container: 'fieldgroupid1'
        });
        processBarcodeInput.isMandatory = true;

        form.addFieldGroup({id: 'fieldgroupid2', label: '工作单信息'});
        var salesOrder = form.addField({
            id: 'custpage_salesorder',
            type: ui.FieldType.TEXT,
            label: '合同号',
            container: 'fieldgroupid2'
        });
        salesOrder.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var wo = form.addField({
            id: 'custpage_wo_num',
            type: ui.FieldType.SELECT,
            label: '工单号',
            source: record.Type.WORK_ORDER,
            container: 'fieldgroupid2'
        });
        wo.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var assemblyitem = form.addField({
            id: 'custpage_assemblyitem',
            type: ui.FieldType.TEXT,
            label: '产品编号',
            //source: record.Type.ASSEMBLY_ITEM,
            container: 'fieldgroupid2'
        });
        assemblyitem.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var itemName = form.addField({
            id: 'custpage_itemname',
            type: ui.FieldType.TEXT,
            label: '产品名称',
            container: 'fieldgroupid2'
        });
        itemName.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var itemSize = form.addField({
            id: 'custpage_itemsize',
            type: ui.FieldType.TEXT,
            label: '尺寸',
            container: 'fieldgroupid2'
        });
        itemSize.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var itemColor = form.addField({
            id: 'custpage_itemcolor',
            type: ui.FieldType.TEXT,
            label: '颜色',
            container: 'fieldgroupid2'
        });
        itemColor.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var itemColorNumber = form.addField({
            id: 'custpage_itemcolornumber',
            type: ui.FieldType.TEXT,
            label: '色号',
            container: 'fieldgroupid2'
        });
        itemColorNumber.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var totalQuantity = form.addField({
            id: 'custpage_realperquantity',
            type: ui.FieldType.INTEGER,
            label: '数量',
            container: 'fieldgroupid2'
        });
        totalQuantity.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var packagePerQuantity = form.addField({
            id: 'custpage_perquantity',
            type: ui.FieldType.INTEGER,
            label: '件/包',
            container: 'fieldgroupid2'
        });
        packagePerQuantity.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var packageCount = form.addField({
            id: 'custpage_packagecount',
            type: ui.FieldType.INTEGER,
            label: '包数',
            container: 'fieldgroupid2'
        });
        packageCount.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var packageNumber = form.addField({
            id: 'custpage_packagenumber',
            type: ui.FieldType.INTEGER,
            label: '包号',
            container: 'fieldgroupid2'
        });
        packageNumber.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var packageNumberQuantity = form.addField({
            id: 'custpage_packagenumberquantity',
            type: ui.FieldType.INTEGER,
            label: '包号对应的数量',
            container: 'fieldgroupid2'
        });
        packageNumberQuantity.updateDisplayType({
            displayType: ui.FieldDisplayType.HIDDEN
        });
        var manuSequence = form.addField({
            id: 'custpage_operationsequence',
            type: ui.FieldType.INTEGER,
            label: '工艺序号',
            container: 'fieldgroupid2'
        });
        manuSequence.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var manuName = form.addField({
            id: 'custpage_operationname',
            type: ui.FieldType.TEXT,
            label: '工艺名称',
            container: 'fieldgroupid2'
        });
        manuName.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var reportworkQuantity = form.addField({
            id: 'custpage_reportworkquantity',
            type: ui.FieldType.INTEGER,
            label: '报工数量',
            container: 'fieldgroupid2'
        });
        reportworkQuantity.isMandatory = true;
        var completedQuantity = form.addField({
            id: 'custpage_completedquantity',
            type: ui.FieldType.INTEGER,
            label: '已完成数量',
            container: 'fieldgroupid2'
        });
        completedQuantity.isMandatory = true;
        var defectiveQuantity = form.addField({
            id: 'custpage_defectivequantity',
            type: ui.FieldType.INTEGER,
            label: '次品数量',
            container: 'fieldgroupid2'
        });
        var crapQuantity = form.addField({
            id: 'custpage_crapquantity',
            type: ui.FieldType.INTEGER,
            label: '废品数量',
            container: 'fieldgroupid2'
        });


        form.addFieldGroup({id: 'fieldgroupid3', label: '创建完工工单状态'});
        var woStatus = form.addField({
            id: 'custpage_wo_status',
            type: ui.FieldType.TEXT,
            label: ' ',
            container: 'fieldgroupid3'
        });
        woStatus.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var iaStatus = form.addField({
            id: 'custpage_ia_status',
            type: ui.FieldType.TEXT,
            label: ' ',
            container: 'fieldgroupid3'
        });
        iaStatus.updateDisplayType({
            displayType: ui.FieldDisplayType.INLINE
        });
        var refreshStatus = form.addField({
            id: 'custpage_refresh_status',
            type: ui.FieldType.CHECKBOX,
            label: ' ',
            container: 'fieldgroupid3'
        });
        refreshStatus.updateDisplayType({
            displayType: ui.FieldDisplayType.HIDDEN
        });


        var completeSublist = form.addSublist({
            id: 'custpage_completedetails',
            type: ui.SublistType.INLINEEDITOR,
            label: '工序完工详情'
        });
        completeSublist.addField({
            id: 'custpage_employee',
            type: ui.FieldType.SELECT,
            label: '员工',
            source: record.Type.EMPLOYEE
        });
        completeSublist.addField({
            id: 'custpage_completedcount',
            type: ui.FieldType.FLOAT,
            label: '完成数量'
        });

        form.addSubmitButton({label: '提交'});
        form.addResetButton({label: '重置'});


        if (context.request.method === 'POST') {
            refreshStatus.defaultValue = 'T';
            log.debug('parameters', context.request.parameters);
            var woId = context.request.parameters.custpage_wo_num;
            if (woId.length > 0) {
                //创建工作完成单
                var woc = record.transform({
                    fromType: 'workorder',
                    fromId: woId,
                    toType: 'workordercompletion',
                    isDynamic: true
                });

                var lookupFields = search.lookupFields({
                    type: 'workorder',
                    id: woId,
                    columns: ['location']
                });
                var location = lookupFields.location[0].value;
                woc.setValue('isbackflush', false); //设置为正向排程
                woc.setValue('custbody_packageno', context.request.parameters.custpage_packagenumber); //包号
                woc.setValue("custbody_actual_issue_location", location);  //实际完工地点
                woc.setValue("custbody_bin", 3);//实际完工库位
                woc.setText("startoperation", context.request.parameters.custpage_process_barcodescan); //开始操作
                woc.setText("endoperation", context.request.parameters.custpage_process_barcodescan);   //结束操作
                woc.setValue("custbody_wo_processnum", context.request.parameters.custpage_process_barcodescan);//自定义的工序号
                woc.setValue("completedquantity", context.request.parameters.custpage_completedquantity);  //已完成数量
                woc.setValue("custbody_reportwork_qty", context.request.parameters.custpage_reportworkquantity);//报工数量
                woc.setValue("custbody_defectivequantity", context.request.parameters.custpage_defectivequantity); //次品数量
                woc.setValue("custbody_processscrapquantity", context.request.parameters.custpage_crapquantity);//废品数量
                var wocId = woc.save();

                var lookupFields1 = search.lookupFields({
                    type: 'workordercompletion',
                    id: wocId,
                    columns: ['tranid']
                });
                var wocTranid = lookupFields1.tranid;

                //工序完工回报
                var count = context.request.getLineCount("custpage_completedetails");
                if (count > 0) {
                    for (var i = 0; i < count; i++) {
                        //员工
                        var employee = context.request.getSublistValue({
                            group: "custpage_completedetails",
                            name: "custpage_employee",
                            line: i
                        });
                        //完成数量
                        var completedCount = context.request.getSublistValue({
                            group: "custpage_completedetails",
                            name: "custpage_completedcount",
                            line: i
                        });
                        //插入新一行
                        var manuCompleteRec = record.create({
                            type: "customrecord65",
                            isDynamic: true
                        });
                        manuCompleteRec.setValue("custrecord_workordercompletenumber", wocId);  //完工单号
                        manuCompleteRec.setValue("custrecord_process", context.request.parameters.custpage_process_barcodescan); //工序
                        manuCompleteRec.setValue("custrecord_employee", employee); //员工
                        manuCompleteRec.setValue("custrecord_quantity", completedCount);//数量
                        manuCompleteRec.setValue("custrecord_workordernumber", woId);   //工单号
                        manuCompleteRec.save();
                    }
                }
                if (wocId) {
                    woStatus.defaultValue = "创建完工工单成功," + wocTranid;
                    if (Number(context.request.parameters.custpage_crapquantity) > 0) {
                        var results = search.create({
                            type: 'inventoryadjustment',
                            columns: ['tranid'],
                            filters: [['custbody_relatedtransation', 'is', wocId]]
                        }).run().getRange(0, 1);
                        if (results.length > 0) {
                            iaStatus.defaultValue = '创建废品库存调整单成功,' + tranid;
                        }
                    }
                } else {
                    woStatus.defaultValue = "创建完工工单失败";
                }
            }


        }
        context.response.writePage(form);

    }

    return {
        onRequest: onRequest
    };
});